/*
 * Copyright (c) 2003, 2004 Ulrich Hilger, http://dev.uhilger.de, all rights reserved.
 *
 * Published under the terms and conditions of the Apache License 2.0,
 * see http://www.apache.org/licenses/LICENSE-2.0
 */

package de.uhilger.app;

import java.util.logging.XMLFormatter;
import java.util.logging.LogRecord;
import java.util.logging.Handler;

/**
 * format log messages for output to an XML file
 *
 * @author Ulrich Hilger, <a href="http://dev.uhilger.de" target="_blank">http://dev.uhilger.de</a>
 * @author Published under the terms and conditions of
 * the <a href="http://www.apache.org/licenses/LICENSE-2.0" target="_blank">Apache License 2.0</a>
 *
 * @version 2, February 12, 2004
 */

public class LogFormatter extends XMLFormatter {

  /**
   * constructor
   */
  public LogFormatter() {
    lineSeparator = "\r\n";/*(String) java.security.AccessController.doPrivileged(
           new sun.security.action.GetPropertyAction("line.separator"));*/
  }

  /**
   * format the given log record
   * @param r LogRecord the record to format
   * @return String the formatted log message
   */
  public String format(LogRecord r) {
    StringBuffer buf = new StringBuffer();
    buf.append("<logrecord time=\"");
    buf.append(r.getMillis());
    buf.append("\"");
    buf.append(" class=\"");
    buf.append(r.getSourceClassName());
    buf.append("\"");
    buf.append(" level=\"");
    buf.append(r.getLevel().getName());
    buf.append("\"");
    buf.append(">");
    buf.append(lineSeparator);
    buf.append(indent);
    buf.append(r.getMessage());
    buf.append(lineSeparator);
    buf.append("</logrecord>");
    buf.append(lineSeparator);
    return buf.toString();
  }

  /**
   * get the header string
   * @param h Handler the handler
   * @return String the header string
   */
  public String getHead(Handler h) {
    return "";
  }

  /**
   * get the trailing string
   * @param h Handler the handler
   * @return String the trailing string
   */
  public String getTail(Handler h) {
    return "";
  }

  /** holds the system dependent line separator */
  private String lineSeparator;

  /** indent space */
  private static final String indent = "  ";
}
